package org.qydjk.console.controller.sys;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.SOrganization;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.vo.Tree;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/organization")
public class SOrganizationController extends BaseController {
    /**
     * “部门管理”入口
     * @return
     */
    @RequestMapping("/manager.do")
    public String organizationManager(){
        return "admin/organization/organization";
    }

    /**
     * 部门资源树
     *
     * @return
     */
    @PostMapping(value = "/tree.do")
    @ResponseBody
    public Object tree() throws Exception {
        List<SOrganization> organizationList = baseService.findByHql("from SOrganization so order by so.seq");
        List<Tree> trees = new ArrayList<Tree>();
        if (organizationList != null) {
            for (SOrganization organization : organizationList) {
                Tree tree = new Tree();
                tree.setId(organization.getId());
                tree.setText(organization.getName());
                tree.setIconCls(organization.getIcon());
                tree.setPid(organization.getPid());
                trees.add(tree);
            }
        }
        return trees;
    }

    /**
     * 部门列表
     *
     * @return
     */
    @RequestMapping("/treeGrid.do")
    @ResponseBody
    public Object treeGrid() throws Exception {
        return baseService.findByHql("from SOrganization so order by so.seq");
    }

    /**
     * 添加部门页
     *
     * @return
     */
    @RequestMapping("/addPage.do")
    public String addPage() {
        return "admin/organization/organizationAdd";
    }

    /**
     * 添加部门
     *
     * @param organization
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object add(@Valid SOrganization organization) throws Exception {
        organization.setCreateTime(new Date());
        baseService.save(organization);
        return renderSuccess("添加成功！");
    }

    /**
     * 编辑部门页
     *
     * @param id
     * @return
     */
    @GetMapping("/editPage.do")
    public String editPage(Model model, Integer id) throws Exception {
        SOrganization organization = baseService.get(SOrganization.class,id);
        model.addAttribute("organization", organization);
        return "admin/organization/organizationEdit";
    }

    /**
     * 编辑部门
     *
     * @param organization
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(@Valid SOrganization organization) throws Exception {
        SOrganization baseO = baseService.get(SOrganization.class, organization.getId());
        BeanUtil.copyProperties(organization,baseO);
        baseService.update(baseO);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(Integer id) throws Exception {
        //子节点必须为空
        int ct = baseService.getRecordCountHQL("select count(o.id) from SOrganization o where o.pid = ?",id.intValue());
        if(ct > 0){
            return renderError("请先将子节点删除");
        }
        SOrganization oo = new SOrganization();
        oo.setId(id);
        baseService.delete(oo);
        return renderSuccess("删除成功！");
    }

}